﻿using BLL;
using Models;
using Inspinia_MVC5.Code;
using Inspinia_MVC5.Models;
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Mvc;

namespace Inspinia_MVC5.Controllers
{
    [AllowCors]
    [SessionTimeOutFilter]
    public class LineController : Controller
    {
        // GET: Line
        public JsonResult Parameter()
        {
            //var list = new List<string> { "井口油压", "井口温度", "井口套压", "汇管压力", "汇管温度", "有功功率", "功率因数", "平衡率", "系统效率", "井口油罐液位", "变频器输出频率", "A相电流有效值", "B相电流有效值", "C相电流有效值", "A相电压有效值", "B相电压有效值", "C相电压有效值", "油井加热炉油温", "伴热温度（抽油机井）", "伴热压力（抽油机井）", "掺稀流量（瞬时量）", "周期内A相电流最大值", "周期内B相电流最大值", "周期内C相电流最大值", "周期内总有功功率最大值", "周期内总无功功率最大值", "周期内总功率因数最大值" };
            var list = new List<string> { "Wellhead oil pressure", "Wellhead oil temperature", "Wellhead casing pressure", "Manifold pressure", "Manifold temperature", "Active power", "Power factor", "Balance rate", "System efficiency", "Wellhead reservoir liquid level", "Converter output frequency", "A-phase current RMS", "B-phase current RMS", "C-phase current RMS", "A-phase voltage RMS", "B-phase voltage RMS", "C-phase voltage RMS", "Heater oil temperature", "Heating temperature", "Heating pressure", "Dilute flow (instantaneous amount)", "A-phase current maximum by period", "B-phase current maximum by period", "C-phase current maximum by period", "Active power maximum by peroid", "Reactive power maximum by peroid", "Power factor maximum by peroid" };
            return Json(new { status = "0", data = list }, JsonRequestBehavior.AllowGet);
        }
        //
        public JsonResult Initialization(string wellid,string parameter)
        {
            string wellstr = Session["wellstr"].ToString();
            string num = "30";
            if (wellstr=="0"||wellstr.Contains("-" + wellid + "-"))
            {
                R_SIGN_Structure rst1 = new R_SIGN_Structure();
                R_SIGN_Structure2Data rssd1 = new R_SIGN_Structure2Data();
                R_SIGN_StructureService R_SIGN_StructureService = new R_SIGN_StructureService();
                R_SIGN_Structure2DataService R_SIGN_Structure2DataService = new R_SIGN_Structure2DataService();
                List<R_SIGN_Structure> list1 = new List<R_SIGN_Structure>();
                list1 = R_SIGN_StructureService.All().ToList();
                foreach (R_SIGN_Structure rst in list1)
                {
                    if (rst.Describe.Trim() == parameter)
                    {
                        rst1.Describe = rst.Describe.Trim();
                        rst1.Structure_ID = rst.Structure_ID;
                        rst1.Structure_Name = rst.Structure_Name;
                        rst1.Address = rst.Address.Trim();
                        rst1.IDCode = rst.IDCode.Trim();
                        rst1.Unit = rst.Unit.Trim();
                        rst1.Scaler = rst.Scaler;
                    }
                }
                List<R_SIGN_Structure2Data> list2 = new List<R_SIGN_Structure2Data>();
                list2 = R_SIGN_Structure2DataService.All().ToList();
                foreach (R_SIGN_Structure2Data rssd in list2)
                {
                    if (rssd.Structure_Name == rst1.Structure_Name)
                    {
                        rssd1.Structure_Name = rssd.Structure_Name;
                        rssd1.DataTable_Name = rssd.DataTable_Name;
                        rssd1.CycleTime = rssd.CycleTime;
                    }
                }
                DataTable dt = ResultHelper.getInitializationLine(wellid, rssd1.DataTable_Name, num);
                if (dt.Rows.Count <= 0)
                {
                    return Json(new { status = "3" }, JsonRequestBehavior.AllowGet);
                }
                List<KeyValueModel> items = new List<KeyValueModel>();
                for (int i = dt.Rows.Count - 1; i >= 0; i--)
                {
                    items.Add(new KeyValueModel { Key = dt.Rows[i]["Time"].ToString(), Value = (Convert.ToDouble(dt.Rows[i][rst1.IDCode].ToString()) * rst1.Scaler).ToString() });
                }
                Session["wellid"] = wellid;
                Session["tablename"] = rssd1.DataTable_Name;
                Session["parameter"] = rst1.IDCode;
                return Json(new { status = "0", data = items, unit = rst1.Unit }, JsonRequestBehavior.AllowGet);
            }
            else
            {
                return Json(new { status = "2" }, JsonRequestBehavior.AllowGet);
            }
        }

        public JsonResult NewLineData()
        {
            string wellstr = Session["wellstr"].ToString();
            string wellid = Session["wellid"].ToString();
            string parameter = Session["parameter"].ToString();
            string tablename = Session["tablename"].ToString();
            if (wellstr == "0" || wellstr.Contains("-" + wellid + "-"))
            {
                DataTable dt = ResultHelper.getNewLineData(wellid, tablename);
                if(dt.Rows.Count <= 0)
                {
                    return Json(new { status = "3" }, JsonRequestBehavior.AllowGet);
                }
                KeyValueModel items = new KeyValueModel { Key = dt.Rows[0]["Time"].ToString(), Value = dt.Rows[0][parameter].ToString() };
                return Json(new { status = "0", data = items }, JsonRequestBehavior.AllowGet);
            }
            else
            {
                return Json(new { status = "2" }, JsonRequestBehavior.AllowGet);
            }
        }
        //
        public JsonResult HistoryLineData(string wellid, string parameter, string starttime, string endtime)
        {
            string wellstr = Session["wellstr"].ToString();
            if (wellstr == "0" || wellstr.Contains("-" + wellid + "-"))
            {
                R_SIGN_Structure rst1 = new R_SIGN_Structure();
                R_SIGN_Structure2Data rssd1 = new R_SIGN_Structure2Data();
                R_SIGN_StructureService R_SIGN_StructureService = new R_SIGN_StructureService();
                R_SIGN_Structure2DataService R_SIGN_Structure2DataService = new R_SIGN_Structure2DataService();
                List<R_SIGN_Structure> list1 = new List<R_SIGN_Structure>();
                list1 = R_SIGN_StructureService.All().ToList();
                foreach (R_SIGN_Structure rst in list1)
                {
                    if (rst.Describe.Trim() == parameter)
                    {
                        rst1.Describe = rst.Describe.Trim();
                        rst1.Structure_ID = rst.Structure_ID;
                        rst1.Structure_Name = rst.Structure_Name;
                        rst1.Address = rst.Address.Trim();
                        rst1.IDCode = rst.IDCode.Trim();
                        rst1.Unit = rst.Unit.Trim();
                        rst1.Scaler = rst.Scaler;
                    }
                }
                List<R_SIGN_Structure2Data> list2 = new List<R_SIGN_Structure2Data>();
                list2 = R_SIGN_Structure2DataService.All().ToList();
                foreach (R_SIGN_Structure2Data rssd in list2)
                {
                    if (rssd.Structure_Name == rst1.Structure_Name)
                    {
                        rssd1.Structure_Name = rssd.Structure_Name;
                        rssd1.DataTable_Name = rssd.DataTable_Name;
                        rssd1.CycleTime = rssd.CycleTime;
                    }
                }
                DataTable dt = ResultHelper.getHistoryLine(wellid, rssd1.DataTable_Name, starttime, endtime);
                if (dt.Rows.Count <= 0)
                {
                    return Json(new { status = "3" }, JsonRequestBehavior.AllowGet);
                }
                List<KeyValueModel> items = new List<KeyValueModel>();
                for (int i = dt.Rows.Count - 1; i >= 0; i--)
                {
                    items.Add(new KeyValueModel { Key = dt.Rows[i]["Time"].ToString(), Value = (Convert.ToDouble(dt.Rows[i][rst1.IDCode].ToString()) * rst1.Scaler).ToString() });
                }
                return Json(new { status = "0", data = items, unit = rst1.Unit }, JsonRequestBehavior.AllowGet);
            }
            else
            {
                return Json(new { status = "2" }, JsonRequestBehavior.AllowGet);
            }
        }
        public JsonResult IndicatorHistoryLineData(string wellid, string para, DateTime starttime, DateTime endtime, int page = 1, int size = 12)
        {
            string wellstr = Session["wellstr"].ToString();
            if (wellstr == "0" || wellstr.Contains("-" + wellid + "-"))
            {
                if (para == "电流")
                {
                    List<SimpleIndicatorModel> SimpleIndicatorModel = new List<SimpleIndicatorModel>();
                    SimpleIndicatorModel = ResultHelper.getDianLiuIndicatorsByTime(wellid, starttime, endtime);
                    if (SimpleIndicatorModel.Count() == 0)
                        return Json(new { status = "3" }, JsonRequestBehavior.AllowGet);
                    return Json(new { status = "0", unit = "A", totalnum = SimpleIndicatorModel.Count(), data = SimpleIndicatorModel.Skip((page - 1) * size).Take(size) }, JsonRequestBehavior.AllowGet);
                }
                else if (para == "有功功率")
                {
                    List<SimpleIndicatorModel> SimpleIndicatorModel = new List<SimpleIndicatorModel>();
                    SimpleIndicatorModel = ResultHelper.getYouGongIndicatorsByTime(wellid, starttime, endtime);
                    if (SimpleIndicatorModel.Count() == 0)
                        return Json(new { status = "3" }, JsonRequestBehavior.AllowGet);
                    return Json(new { status = "0", unit = "W", totalnum = SimpleIndicatorModel.Count(), data = SimpleIndicatorModel.Skip((page - 1) * size).Take(size) }, JsonRequestBehavior.AllowGet);
                }
                return Json(new { status = "4" }, JsonRequestBehavior.AllowGet);
            }
            else
            {
                return Json(new { status = "2" }, JsonRequestBehavior.AllowGet);
            }
        }
        public ActionResult RealLine()
        {
            return View();
        }
        public ActionResult HistoryLine()
        {
            return View();
        }
        public ActionResult IndicatorHistoryLine()
        {
            return View();
        }
    }
}